home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windows 95 API Bible
/
Windows 95 API Bible 3 Disc Set.iso
/
Win32 API Bible Book 1 of 3.iso
/
chapte25
/
ex19.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-04-24
|
5KB
|
117 lines
#include <genstub.c>
#define IDM_LAUNCH 201
#define IDM_READ 202
#define IDM_WRITE 203
#define IDM_FLUSH 204
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static LPVOID lpHeapData = NULL;
static LPVOID lpExtData = NULL;
static DWORD dwDebuggeeProcessID = 0;
switch (uMsg)
{
case WM_CREATE:
{
DefWindowProc(hWnd, uMsg, wParam, lParam);
// allocate some heap data for exchange
lpHeapData = HeapAlloc( GetProcessHeap(), 0, 256 );
// if child set child name.
if ( strstr( GetCommandLine(), "DEBUGGEE" ) )
{
SetWindowText( hWnd, "DEBUGGEE" );
dwDebuggeeProcessID = GetCurrentProcessId( );
lstrcpy( lpHeapData, "@@@ Debuggee Data @@@");
SetMenu( hWnd, 0 ); // Debuggee doesn't need a menu.
}
else
{ // position parent above child
SetWindowText( hWnd, "DEBUGGER" );
lstrcpy(lpHeapData, "<<<< Debugger Data >>>>");
}
}
break;
case WM_DESTROY:
HeapFree( GetProcessHeap(), 0, lpHeapData );
PostQuitMessage( 0 );
break;
case WM_COMMAND: // process menu items
{
TCHAR szBuffer[128];
switch ( LOWORD( wParam ) )
{
case IDM_LAUNCH:
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
HWND hWndEx;
// only one can be debugged at a time.
if (FindWindow(lpszAppName, "DEBUGGEE")!=0)
break;
ZeroMemory(&si, sizeof(STARTUPINFO));
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
si.cb=sizeof(STARTUPINFO);
CreateProcess( "E:\\WAITE\\CHAP25\\GENERIC.EXE", "DEBUGGEE", NULL, NULL, FALSE,
0, NULL, NULL, &si, &pi );
dwDebuggeeProcessID = pi.dwProcessId;
// Let initialization of new process occur.
if ( pi.dwProcessId )
{
WaitForInputIdle( GetCurrentProcess(), INFINITE );
while ((hWndEx = FindWindow(lpszAppName, "DEBUGGEE"))==0);
lpExtData = (LPVOID) SendMessage(hWndEx, WM_USER, 0, 0);
}
}
break;
case IDM_READ:
{
TCHAR szData[256];
DWORD dwBytesRead = 0;
HANDLE hDebuggee = OpenProcess( PROCESS_VM_READ, FALSE,
dwDebuggeeProcessID );
ReadProcessMemory( hDebuggee, lpExtData,
szData, 255, &dwBytesRead );
wsprintf( szBuffer, "%ld Bytes Read. Data=%s",
dwBytesRead, szData );
MessageBox( hWnd, szBuffer, "Reading Process", MB_OK );
CloseHandle( hDebuggee );
}
break;
case IDM_WRITE:
{
DWORD dwBytesWritten = 0;
HANDLE hDebuggee = OpenProcess( PROCESS_VM_WRITE, FALSE,
dwDebuggeeProcessID );
WriteProcessMemory( hDebuggee, lpExtData,
lpHeapData, 255, &dwBytesWritten );
wsprintf(szBuffer, "%ld Bytes Written", dwBytesWritten);
MessageBox( hWnd, lpExtData, "Writing Process", MB_OK );
CloseHandle( hDebuggee );
}
break;
case IDM_FLUSH:
{
HANDLE hDebuggee = OpenProcess( PROCESS_VM_WRITE, FALSE,
dwDebuggeeProcessID );
wsprintf( szBuffer, "Flush returns: %d ",
FlushInstructionCache( hDebuggee, NULL, 100 ) );
MessageBox( hWnd, szBuffer, "FlushInstructionCache()", MB_OK );
CloseHandle( hDebuggee );
}
break;
case IDM_EXIT:
DestroyWindow( hWnd );
break;
}
}
break;
case WM_USER: // Used to return value of the heap data.
return lpHeapData;
default:
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
return NULL;
}